﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKS.ServiceReferenceQLKS;
namespace QLKS
{
    public partial class FrmLoaiPhong : Form
    {
        private IList<LOAIPHONG> lstloaiphong;
        private bool ThemOrCapNhat = false;
        public static FrmLoaiPhong frm = null;

        public FrmLoaiPhong()
        {
            InitializeComponent();
            frm = this;
            InitLoaiPhong();
        }
        private void InitLoaiPhong()
        {
            var client=new ServiceQLKSClient();
            LOAIPHONG[] outloaiphong;
            var error = client.doparserLoaiPhong(out outloaiphong, OPTCodeQLKS.OPTCode_LOAIPHONG_GETALL, null);
            if (error == ErrorCodeQLKS.CODE_LOAIPHONG_GETALL_SUCCESS)
            {
                lstloaiphong = outloaiphong.ToList();
                lOAIPHONGBindingSource.DataSource = lstloaiphong;
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void InsertOrUpdateLoaiPhong()
        {
            LOAIPHONG loaiphong = new LOAIPHONG();
                if (txtMaLoaiP.Text == null || txtLoaiP.Text == null || txtGia.Text == null)
                {
                    MessageBox.Show("Lỗi dữ liệu nhập vào!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    loaiphong.MSLOAIPHONG = txtMaLoaiP.Text;
                    loaiphong.TENLOAIPHONG = txtLoaiP.Text;
                    loaiphong.GIATIEN = decimal.Parse(txtGia.Text);
                }
            LOAIPHONG[] outloaiphong;
            var client = new ServiceQLKSClient();
            var error = client.doparserLoaiPhong(out outloaiphong, OPTCodeQLKS.OPTCode_LOAIPHONG_INSERTUPDATE, loaiphong);
            if (error == ErrorCodeQLKS.CODE_LOAIPHONG_INSERTUPDATE_SUCCESS)
            {
                InitLoaiPhong();
                gvLoaiPhong.ClearSelection();
                txtMaLoaiP.Enabled = false;
                var item=outloaiphong.FirstOrDefault();
                for (int i = 0; i < gvLoaiPhong.RowCount; i++)
                {
                    if (gvLoaiPhong.Rows[i].Cells[0].Value.ToString().Equals(item.MSLOAIPHONG))
                    {
                        gvLoaiPhong.Rows[i].Selected = true;
                        return;
                    }
                }
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }

            ThemOrCapNhat = false;

        }

        private void deletePhong()
        {
            LOAIPHONG[] outloaiphong;
            var client = new ServiceQLKSClient();
            var error = client.doparserLoaiPhong(out outloaiphong, OPTCodeQLKS.OPTCode_LOAIPHONG_DELETE, new LOAIPHONG { MSLOAIPHONG = txtMaLoaiP.Text });
            if (error == ErrorCodeQLKS.CODE_NHANVIEN_DELETE_SUCCESS)
            {
                InitLoaiPhong();
            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            txtMaLoaiP.Enabled = true;
            txtMaLoaiP.Clear();
            txtMaLoaiP.Focus();
            txtLoaiP.Clear();
            txtGia.Clear();
            ThemOrCapNhat = true;
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            InsertOrUpdateLoaiPhong();
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            deletePhong();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            txtMaLoaiP.Clear();
            txtMaLoaiP.Focus();
            txtLoaiP.Clear();
            txtGia.Clear();
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmLoaiPhong_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm = null;
        }
    }
}
